/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.bry.login;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JButton;
import org.openide.DialogDisplayer;
import org.openide.LifecycleManager;
import org.openide.NotifyDescriptor;
import org.openide.modules.ModuleInstall;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;
import org.openide.windows.WindowManager;

public class Installer extends ModuleInstall implements ActionListener, LookupListener{
	LoginForm form;

	@Override
	protected void initialize() {
		
		super.initialize(); //To change body of generated methods, choose Tools | Templates.
//		associ
//		System.setProperty("netbeans.buildnumber", 3.4.5); 
		
	}
	
	private void showLoginForm() {
		form = new LoginForm();
		
//String initialValue = "Purchase";
//String cashOut = "Cashout";
//String[] options = new String[]{initialValue, cashOut};
//
//NotifyDescriptor nd = new NotifyDescriptor(
//            form,
//            "Title",
//            NotifyDescriptor.PLAIN_MESSAGE,
//            NotifyDescriptor.PLAIN_MESSAGE,
//            null,
//            null
//    );

//	new NotifyDescriptor(cashOut, cashOut, optionType, messageType, options, initialValue)
		
		NotifyDescriptor nd = new NotifyDescriptor.Confirmation(form,"Login");
		nd.addPropertyChangeListener(new PropertyChangeListener(){
			@Override	
			public void propertyChange(PropertyChangeEvent e){
				if(e.getNewValue().equals(NotifyDescriptor.CLOSED_OPTION)){
				    LifecycleManager.getDefault().exit();
				}
				if(e.getNewValue().equals(NotifyDescriptor.OK_OPTION)) {
					
				}
			}
		});
		JButton ok = new JButton();
		ok.setText("Login");
		JButton cancel = new JButton();
		cancel.setText("Cancel");
		ok.addActionListener(this);
//		
		cancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
			    LifecycleManager.getDefault().exit();
			}
		});
		nd.setOptions(new Object[] {ok, cancel});
//		new NotifyDescriptor.Message(this, messageType)
		
//		NotifyDescriptor.Message dd = new DialogDescriptor.Message(form, DialogDescriptor.INFORMATION_MESSAGE);
//		new DialogDescriptor.Message(this);
//		DialogDisplayer dlg = DialogDisplayer.getDefault().
		DialogDisplayer.getDefault().notifyLater(nd);
//		Dialog dlg = DialogDisplayer.getDefault().createDialog(dd);
//		dlg.setResizable(false);
//		dlg.setVisible(true);
		
//		Confirmation msg = new NotifyDescriptor.Confirmation("some string"); 
//		Object result = DialogDisplayer.getDefault().notify(msg); 
	}
	
	@Override
	public void restored() {
		this.showLoginForm();
//		WindowManager.getDefault().findTopComponent("output").putClientProperty("netbeans.winsys.tc.closing_disabled", Boolean.TRUE);
//		System.setProperty("netbeans.buildnumber", "3.4.5");
		
//		SwingUtilities.invokeLater(new Runnable() {
//		    public void run() {
//			Frame f = WindowManager.getDefault().getMainWindow();
//			f.addWindowListener(new Listener());
//		    }
//		});
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String userName = form.getUserName();
		String password = form.getPassword();
		User user = Lookup.getDefault().lookup(User.class);
		user.setUsername(userName);
		user.setPassword(password);
		if(!user.isAuthenticate()) {
//			WindowManager.getDefault().getMainWindow().setVisible(false);
//			JOptionPane.showMessageDialog(null,"Not authorize");
//		WindowManager.getDefault().invokeWhenUIReady(new Runnable() {
//		    public void run() {
//			    WindowManager.getDefault().getMainWindow().setVisible(false);
//			// any code here will be run with the UI is available
////			SomeTopComponent.findInstance().open();
////			JFrame f = (JFrame) WindowManager.getDefault().getMainWindow(); 
////			f.setVisible(false);
////			f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
//		    }
//		});
//			this.showLoginForm();
		}
	}

	@Override
	public void resultChanged(LookupEvent ev) {
		
	}
	//Listener for setting size, because the size is reset after installation:
	public class Listener extends WindowAdapter {
	    @Override
	    public void windowActivated(WindowEvent event) {
		Frame f =  WindowManager.getDefault().getMainWindow();
		f.setResizable(false);
		f.setSize(600, 400);
	    }
	}
	
	

	@Override
	public boolean closing() {
		return super.closing(); //To change body of generated methods, choose Tools | Templates.
	}

	
	
}
